package com.cse210.grocerwe.server;
import org.restlet.resource.ServerResource;
import org.restlet.resource.Post;  

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import com.cse210.grocerwe.common.*;

import org.restlet.data.Form;
import org.restlet.representation.Representation;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class DBResource extends ServerResource {
	
    @Post  
    public Representation acceptItem(Representation entity) {  
    	
        Representation result = null;
        Form form = new Form(entity);
        String mode = form.getFirstValue("mode");
        PersistenceManager pm = PMF.get().getPersistenceManager();
        
        if(mode.equals("add"))
        {
	        String name = form.getFirstValue("name");
	        String type = form.getFirstValue("type");
	    	ShoppingItem si = new ShoppingItem(name,type);
	    	
	        try {
	            pm.makePersistent(si);
	        } finally {
	            pm.close();
	        }
	
	        redirectSeeOther("/db");
        }
        else if(mode.equals("remove"))
        {
        	String key = form.getFirstValue("key");
        	Key k = KeyFactory.stringToKey(key);
        	ShoppingItem rm = pm.getObjectById(ShoppingItem.class,k);
        	
        	pm.deletePersistent(rm);
        	redirectSeeOther("/db");
        }
        else if(mode.equals("addGroup"))
        {
	        String user = form.getFirstValue("user");
	        String name = form.getFirstValue("name");
	    	ShoppingGroup sg = new ShoppingGroup(name,user,false);
	        
	        try {
	            pm.makePersistent(sg);
		    	MapUserToGroup mug = new MapUserToGroup(user,sg.getKey());
	            pm.makePersistent(mug);
	        } finally {
	            pm.close();
	        }
	
        	redirectSeeOther("/groups");
        }
        else if(mode.equals("removeGroup"))
        {
        	String key = form.getFirstValue("key");
        	Key k = KeyFactory.stringToKey(key);
        	ShoppingGroup rm = pm.getObjectById(ShoppingGroup.class,k);
        	
            Query query = pm.newQuery(MapUserToGroup.class);
            query.setFilter("group == k");
            query.declareParameters("String k");
            query.deletePersistentAll(key);
            
        	pm.deletePersistent(rm);
        	redirectSeeOther("/groups");
        }
        else if(mode.equals("acceptRequest"))
        {
        	String key = form.getFirstValue("key");
        	String acceptinguser = form.getFirstValue("user");
        	String group = form.getFirstValue("group");
        	
        	MapUserToGroup mug = new MapUserToGroup(acceptinguser, group);
	        try {
	            pm.makePersistent(mug);
		        pm.deletePersistent(pm.getObjectById(GroupRequest.class,KeyFactory.stringToKey(key)));
	        } finally {
	            pm.close();
	        }
        	redirectSeeOther("/start");
	
        }
        else if(mode.equals("rejectRequest"))
        {
        	String key = form.getFirstValue("key");
        	pm.deletePersistent(pm.getObjectById(GroupRequest.class,KeyFactory.stringToKey(key)));
        	redirectSeeOther("/start");
        }
        else if(mode.equals("kickuser"))
        {
        	String entry = form.getFirstValue("entry");
        	MapUserToGroup mug = pm.getObjectById(MapUserToGroup.class,KeyFactory.stringToKey(entry));
        	pm.deletePersistent(mug);
        	redirectSeeOther("/start");
        }
        else if(mode.equals("inviteuser"))
        {
        	String group = form.getFirstValue("group");
        	String owner = form.getFirstValue("owner");
        	String groupname = form.getFirstValue("groupname");
        	String user = form.getFirstValue("user");
        	
        	GroupRequest gr = new GroupRequest(user.toLowerCase(), owner, groupname, group);
	        try {
	            pm.makePersistent(gr);
	        } finally {
	            pm.close();
	        }
        	redirectSeeOther("/start");
        }
        else if(mode.equals("addListItem"))
        {
        	String group = form.getFirstValue("group");
        	String owner = form.getFirstValue("owner");
	        String val = form.getFirstValue("name");
	        String [] vals = val.split("~");
	        
	    	ShoppingItem si = new ShoppingItem(vals[0],vals[1],owner,group);
	    	
	        try {
	            pm.makePersistent(si);
	        } finally {
	            pm.close();
	        }
	
        	redirectSeeOther("/start");
        }
        else if(mode.equals("removeFromList"))
        {
        	String key = form.getFirstValue("key");
        	Key k = KeyFactory.stringToKey(key);
        	ShoppingItem rm = pm.getObjectById(ShoppingItem.class,k);
        	
        	pm.deletePersistent(rm);
        	redirectSeeOther("/start");
        }
        
        return result;  
    }  
  

}
